package com.monkeygroup.mygoplus.view.home.activity.person;

import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.mob.tools.utils.UIHandler;
import com.monkeygroup.mygoplus.R;
import com.monkeygroup.mygoplus.base.BaseActivity;
import com.monkeygroup.mygoplus.http.RequestUrl;
import com.monkeygroup.mygoplus.user.vo.AccountInfo;
import com.monkeygroup.mygoplus.view.entrance.fragment.LoginThirdFragment;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import java.util.HashMap;

import cn.sharesdk.facebook.Facebook;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.google.GooglePlus;
import cn.sharesdk.line.Line;
import cn.sharesdk.wechat.friends.Wechat;
import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by cumt on 2017/6/30.
 */

public class PersonBindThirdActivity extends BaseActivity implements View.OnClickListener,Handler.Callback,PlatformActionListener {
    private final String TAG = "PersonBindThirdActivity";
    public static void gotoActivity(Activity activity) {
        Intent intent = new Intent(activity,
                PersonBindThirdActivity.class);

        activity.startActivity(intent);
    }

    private Activity activity;
    private ImageView imgBack;
    private TextView txtTitle;

    private static final int MSG_LOGIN = 1;
    private static final int MSG_AUTH_CANCEL = 2;
    private static final int MSG_AUTH_ERROR= 3;
    private static final int MSG_AUTH_COMPLETE = 4;

    private Handler handler;

    private TextView txt_facebook_connect,txt_wechat_connect,txt_Line_connect,txt_google_connect,
            txt_facebook_cancel,txt_wechat_cancel,txt_Line_cancel,txt_google_cancel;

    @Override
    public void onClick(View view) {
        LoginThirdFragment login = new LoginThirdFragment();
        switch (view.getId()) {
            case R.id.txt_facebook_connect:
                Platform facebookPlat = ShareSDK.getPlatform(Facebook.NAME);
                login.authorize(facebookPlat);
                break;
            case R.id.txt_wechat_connect:
                Platform wechatPlat = ShareSDK.getPlatform(Wechat.NAME);
                login.authorize(wechatPlat);
                break;
            case R.id.txt_Line_connect:
                Platform linePlat = ShareSDK.getPlatform(Line.NAME);
                login.authorize(linePlat);
                break;
            case R.id.txt_google_connect:
                Platform googlePlat = ShareSDK.getPlatform(GooglePlus.NAME);
                login.authorize(googlePlat);
                break;
            case R.id.txt_facebook_cancel:

                break;
            case R.id.txt_wechat_cancel:

                break;
            case R.id.txt_Line_cancel:

                break;
            case R.id.txt_google_cancel:

                break;
            default:
                break;
        }
    }

    @Override
    public void setContentView() {
        setContentView(R.layout.person_bind_third);
    }

    @Override
    public void initView() {
        activity=PersonBindThirdActivity.this;
        ShareSDK.initSDK(activity.getApplicationContext(),"1c615185e4e7e");
        handler = new Handler(this);
        imgBack = (ImageView) findViewById(R.id.img_back);
        txtTitle = (TextView) findViewById(R.id.txt_title);
        txtTitle.setText("綁定賬號");

        txt_facebook_connect= (TextView)findViewById(R.id.txt_facebook_connect);
        txt_wechat_connect= (TextView)findViewById(R.id.txt_wechat_connect);
        txt_Line_connect= (TextView)findViewById(R.id.txt_Line_connect);
        txt_google_connect= (TextView)findViewById(R.id.txt_google_connect);



    }

    @Override
    public void setListener() {
        findViewById(R.id.txt_facebook_connect).setOnClickListener(this);
    }

    @Override
    public void doEvent() {

    }

    @Override
    public boolean handleMessage(Message message) {
        return false;
    }

    @Override
    public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
        if (i == Platform.ACTION_USER_INFOR) {
            Message msg = new Message();
            msg.what = MSG_AUTH_COMPLETE;
            msg.obj = new Object[] {platform.getName(), hashMap};
            handler.sendMessage(msg);
            bindAccount(platform.getName(),platform.getDb().getUserName());
            // 取消授权
            platform.removeAccount(true);
        }
    }

    @Override
    public void onError(Platform platform, int i, Throwable throwable) {
        if (i == Platform.ACTION_USER_INFOR) {
            handler.sendEmptyMessage(MSG_AUTH_ERROR);
        }
        throwable.printStackTrace();
    }

    @Override
    public void onCancel(Platform platform, int i) {
        if (i == Platform.ACTION_USER_INFOR) {
            handler.sendEmptyMessage(MSG_AUTH_CANCEL);
        }
    }

    // 业务逻辑：绑定账号
    private void bindAccount(String plat, String userName) {
        Message msg = new Message();
        msg.what    = MSG_LOGIN;
        msg.obj     = plat;
        UIHandler.sendMessage(msg, this);
        OkHttpUtils.post().url(RequestUrl.isbind).addParams("acc",userName).build().execute(new Callback() {
            @Override
            public Object parseNetworkResponse(Response response, int i) throws Exception {
                return null;
            }

            @Override
            public void onError(Call call, Exception e, int i) {
                Log.e(TAG,"访问失败");
            }

            @Override
            public void onResponse(Object o, int i) {
                Gson gson = new Gson();
                AccountInfo accountInfo = gson.fromJson(gson.toJson(o), new TypeToken<AccountInfo>() {}.getType());
                Log.e(TAG,accountInfo.getAppacc());
            }
        });
    }
}
